/*
 * Storage二次封装
 *
 */
import config from './../config'
export default {
  setItem(key, val) {
    let storage = this.getStoreage()
    // console.log(storage)
    // {"name":"tom","age":"30"}
    // let obj = {[key]:val}
    storage[key] = val
    // JSON.stringify将对象转换成JSON字符串  存储方式为(命名空间, JSON字符串格式的数据)
    window.localStorage.setItem(config.namespace, JSON.stringify(storage))
  },
  // 获取命名空间的单个属性和属性值
  getItem(key) {
    return this.getStoreage()[key]
  },
  getStoreage() {
    // 取命名空间
    return JSON.parse(window.localStorage.getItem(config.namespace) || '{}')
  },
  clearItem(key) {
    let storage = this.getStoreage()
    delete storage[key]
    window.localStorage.setItem(config.namespace, JSON.stringify(storage))
  },
  clearAll() {
    window.localStorage.clear()
  }
}
